home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / doc.c < prev    next >
C/C++ Source or Header  |  1993-12-02  |  16KB  |  559 lines

  1. /* Record indices of function doc strings stored in a file.
  2.    Copyright (C) 1985, 1986, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22.  
  23. #include <sys/types.h>
  24. #ifndef    WINDOWSNT
  25. #include <sys/file.h>    /* Must be after sys/types.h for USG and BSD4_1*/
  26. #else  /* WINDOWSNT */
  27. #include <fcntl.h>
  28. #endif /* WINDOWSNT */
  29.  
  30. #ifdef USG5
  31. #include <fcntl.h>
  32. #endif
  33.  
  34. #ifndef O_RDONLY
  35. #define O_RDONLY 0
  36. #endif
  37.  
  38. #include "lisp.h"
  39. #include "buffer.h"
  40. #include "keyboard.h"
  41.  
  42. #ifdef STDC_HEADERS
  43. #include <stdlib.h>
  44. #include <io.h>
  45. #include <string.h>
  46. #endif
  47. #include "doc_p.h"
  48. #include "alloca_p.h"
  49. #include "insdel_p.h"
  50. static void store_function_docstring _P_((Lisp_Object fun, int offset));
  51.  
  52. Lisp_Object Vdoc_file_name;
  53.  
  54. Lisp_Object
  55. get_doc_string (filepos)
  56.      long filepos;
  57. {
  58.   char buf[512 * 32 + 1];
  59.   register int fd;
  60.   register char *name;
  61.   register char *p, *p1;
  62.   register int count;
  63. #ifndef USE_PROTOTYPES
  64.   extern char *index ();
  65. #endif
  66.  
  67.   if (XTYPE (Vdata_directory) != Lisp_String
  68.       || XTYPE (Vdoc_file_name) != Lisp_String)
  69.     return Qnil;
  70.  
  71.   name = (char *) alloca (XSTRING (Vdata_directory)->size
  72.               + XSTRING (Vdoc_file_name)->size + 8);
  73.   strcpy (name, XSTRING (Vdata_directory)->data);
  74.   strcat (name, XSTRING (Vdoc_file_name)->data);
  75. #ifdef VMS
  76. #ifndef VMS4_4
  77.   /* For VMS versions with limited file name syntax,
  78.      convert the name to something VMS will allow.  */
  79.   p = name;
  80.   while (*p)
  81.     {
  82.       if (*p == '-')
  83.     *p = '_';
  84.       p++;
  85.     }
  86. #endif /* not VMS4_4 */
  87. #ifdef VMS4_4
  88.   strcpy (name, sys_translate_unix (name));
  89. #endif /* VMS4_4 */
  90. #endif /* VMS */
  91.  
  92.   fd = open (name, O_RDONLY, 0);
  93.   if (fd < 0)
  94.     error ("Cannot open doc string file \"%s\"", name);
  95.   if (0 > lseek (fd, filepos, 0))
  96.     {
  97.       close (fd);
  98.       error ("Position %ld out of range in doc string file \"%s\"",
  99.          filepos, name);
  100.     }
  101.   p = buf;
  102.   while (p != buf + sizeof buf - 1)
  103.     {
  104.       count = read (fd, p, 512);
  105.       p[count] = 0;
  106.       if (!count)
  107.     break;
  108.       p1 = index (p, '\037');
  109.       if (p1)
  110.     {
  111.       *p1 = 0;
  112.       p = p1;
  113.       break;
  114.     }
  115.       p += count;
  116.     }
  117.   close (fd);
  118.   return make_string (buf, p - buf);
  119. }
  120.  
  121. DEFUN ("documentation", Fdocumentation, Sdocumentation, 1, 2, 0,
  122.   "Return the documentation string of FUNCTION.\n\
  123. Unless a non-nil second argument is given, the\n\
  124. string is passed through `substitute-command-keys'.")
  125.   (function, raw)
  126.      Lisp_Object function, raw;
  127. {
  128.   Lisp_Object fun;
  129.   Lisp_Object funcar;
  130.   Lisp_Object tem, doc;
  131.  
  132.   fun = Findirect_function (function);
  133.  
  134.   switch (XTYPE (fun))
  135.     {
  136.     case Lisp_Subr:
  137.       if (XSUBR (fun)->doc == 0) return Qnil;
  138.       if ((int) XSUBR (fun)->doc >= 0)
  139.     doc = build_string (XSUBR (fun)->doc);
  140.       else
  141.     doc = get_doc_string (- (int) XSUBR (fun)->doc);
  142.       break;
  143.       
  144.     case Lisp_Compiled:
  145.       if (XVECTOR (fun)->size <= COMPILED_DOC_STRING)
  146.     return Qnil;
  147.       tem = XVECTOR (fun)->contents[COMPILED_DOC_STRING];
  148.       if (XTYPE (tem) == Lisp_String)
  149.     doc = tem;
  150.       else if (XTYPE (tem) == Lisp_Int && XINT (tem) >= 0)
  151.     doc = get_doc_string (XFASTINT (tem));
  152.       else
  153.     return Qnil;
  154.       break;
  155.  
  156.     case Lisp_String:
  157.     case Lisp_Vector:
  158.       return build_string ("Keyboard macro.");
  159.  
  160.     case Lisp_Cons:
  161.       funcar = Fcar (fun);
  162.       if (XTYPE (funcar) != Lisp_Symbol)
  163.     return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
  164.       else if (EQ (funcar, Qkeymap))
  165.     return build_string ("Prefix command (definition is a keymap associating keystrokes with\n\
  166. subcommands.)");
  167.       else if (EQ (funcar, Qlambda)
  168.            || EQ (funcar, Qautoload))
  169.     {
  170.       tem = Fcar (Fcdr (Fcdr (fun)));
  171.       if (XTYPE (tem) == Lisp_String)
  172.         doc = tem;
  173.       else if (XTYPE (tem) == Lisp_Int && XINT (tem) >= 0)
  174.         doc = get_doc_string (XFASTINT (tem));
  175.       else
  176.         return Qnil;
  177.  
  178.       break;
  179.     }
  180.       else if (EQ (funcar, Qmocklisp))
  181.     return Qnil;
  182.       else if (EQ (funcar, Qmacro))
  183.     return Fdocumentation (Fcdr (fun), raw);
  184.  
  185.       /* Fall through to the default to report an error.  */
  186.  
  187.     default:
  188.       return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
  189.     }
  190.  
  191.   if (NILP (raw))
  192.     {
  193.       struct gcpro gcpro1;
  194.  
  195.       GCPRO1 (doc);
  196.       doc = Fsubstitute_command_keys (doc);
  197.       UNGCPRO;
  198.     }
  199.   return doc;
  200. }
  201.  
  202. DEFUN ("documentation-property", Fdocumentation_property, Sdocumentation_property, 2, 2, 0,
  203.   "Return the documentation string that is SYMBOL's PROP property.\n\
  204. This is like `get', but it can refer to strings stored in the\n\
  205. `etc/DOC' file; and if the value is a string, it is passed through\n\
  206. `substitute-command-keys'.  A non-nil third argument avoids this\n\
  207. translation.")
  208.   (sym, prop, raw)
  209.      Lisp_Object sym, prop, raw;
  210. {
  211.   register Lisp_Object tem;
  212.  
  213.   tem = Fget (sym, prop);
  214.   if (XTYPE (tem) == Lisp_Int)
  215.     tem = get_doc_string (XINT (tem) > 0 ? XINT (tem) : - XINT (tem));
  216.   if (NILP (raw) && XTYPE (tem) == Lisp_String)
  217.     return Fsubstitute_command_keys (tem);
  218.   return tem;
  219. }
  220.  
  221. /* Scanning the DOC files and placing docstring offsets into functions.  */
  222.  
  223. static void
  224. store_function_docstring (fun, offset)
  225.      Lisp_Object fun;
  226.      int offset;
  227. {
  228.   fun = indirect_function (fun);
  229.  
  230.   /* The type determines where the docstring is stored.  */
  231.  
  232.   /* Lisp_Subrs have a slot for it.  */
  233.   if (XTYPE (fun) == Lisp_Subr)
  234.     XSUBR (fun)->doc = (char *) - offset;
  235.  
  236.   /* If it's a lisp form, stick it in the form.  */
  237.   else if (CONSP (fun))
  238.     {
  239.       Lisp_Object tem;
  240.  
  241.       tem = XCONS (fun)->car;
  242.       if (EQ (tem, Qlambda) || EQ (tem, Qautoload))
  243.     {
  244.       tem = Fcdr (Fcdr (fun));
  245.       if (CONSP (tem) &&
  246.           XTYPE (XCONS (tem)->car) == Lisp_Int)
  247.         XFASTINT (XCONS (tem)->car) = offset;
  248.     }
  249.       else if (EQ (tem, Qmacro))
  250.     store_function_docstring (XCONS (fun)->cdr, offset);
  251.     }
  252.  
  253.   /* Bytecode objects sometimes have slots for it.  */
  254.   else if (XTYPE (fun) == Lisp_Compiled)
  255.     {
  256.       /* This bytecode object must have a slot for the
  257.      docstring, since we've found a docstring for it.  */
  258.       if (XVECTOR (fun)->size <= COMPILED_DOC_STRING)
  259.     abort ();
  260.  
  261.       XFASTINT (XVECTOR (fun)->contents[COMPILED_DOC_STRING]) = offset;
  262.     }
  263. }
  264.  
  265.  
  266. DEFUN ("Snarf-documentation", Fsnarf_documentation, Ssnarf_documentation,
  267.   1, 1, 0,
  268.   "Used during Emacs initialization, before dumping runnable Emacs,\n\
  269. to find pointers to doc strings stored in `etc/DOC...' and\n\
  270. record them in function definitions.\n\
  271. One arg, FILENAME, a string which does not include a directory.\n\
  272. The file is found in `../etc' now; found in the `data-directory'\n\
  273. when doc strings are referred to later in the dumped Emacs.")
  274.   (filename)
  275.      Lisp_Object filename;
  276. {
  277.   int fd;
  278.   char buf[1024 + 1];
  279.   register int filled;
  280.   register int pos;
  281.   register char *p, *end;
  282.   Lisp_Object sym;
  283.   char *name;
  284. #ifndef USE_PROTOTYPES
  285.   extern char *index ();
  286. #endif
  287. #ifdef WINDOWSNT
  288.   int filler = 0;
  289. #endif /* WINDOWSNT */
  290.  
  291. #ifndef CANNOT_DUMP
  292.   if (NILP (Vpurify_flag))
  293.     error ("Snarf-documentation can only be called in an undumped Emacs");
  294. #endif
  295.  
  296.   CHECK_STRING (filename, 0);
  297.  
  298. #ifndef CANNOT_DUMP
  299.   name = (char *) alloca (XSTRING (filename)->size + 14);
  300.   strcpy (name, "../etc/");
  301. #else /* CANNOT_DUMP */
  302.   CHECK_STRING (Vdata_directory, 0);
  303.   name = (char *) alloca (XSTRING (filename)->size +
  304.               XSTRING (Vdata_directory)->size + 1);
  305.   strcpy (name, XSTRING (Vdata_directory)->data);
  306. #endif /* CANNOT_DUMP */
  307.   strcat (name, XSTRING (filename)->data);     /*** Add this line ***/
  308. #ifdef VMS
  309. #ifndef VMS4_4
  310.   /* For VMS versions with limited file name syntax,
  311.      convert the name to something VMS will allow.  */
  312.   p = name;
  313.   while (*p)
  314.     {
  315.       if (*p == '-')
  316.     *p = '_';
  317.       p++;
  318.     }
  319. #endif /* not VMS4_4 */
  320. #ifdef VMS4_4
  321.   strcpy (name, sys_translate_unix (name));
  322. #endif /* VMS4_4 */
  323. #endif /* VMS */
  324.  
  325. #ifdef WINDOWSNT
  326.   fd = open (name, O_RDONLY | O_BINARY, 0);
  327. #else
  328.   fd = open (name, O_RDONLY, 0);
  329. #endif
  330.   if (fd < 0)
  331.     report_file_error ("Opening doc string file",
  332.                Fcons (build_string (name), Qnil));
  333.   Vdoc_file_name = filename;
  334.   filled = 0;
  335.   pos = 0;
  336.   while (1)
  337.     {
  338.       if (filled < 512)
  339.     filled += read (fd, &buf[filled], sizeof buf - 1 - filled);
  340.       if (!filled)
  341.     break;
  342.  
  343.       buf[filled] = 0;
  344.       p = buf;
  345.       end = buf + (filled < 512 ? filled : filled - 128);
  346.       while (p != end && *p != '\037') p++;
  347.       /* p points to ^_Ffunctionname\n or ^_Vvarname\n.  */
  348.       if (p != end)
  349.     {
  350.       end = index (p, '\n');
  351. #ifdef WINDOWSNT
  352.       sym = oblookup (Vobarray, p + 2, end - p - 3);
  353. #else  /* !WINDOWSNT */
  354.       sym = oblookup (Vobarray, p + 2, end - p - 2);
  355. #endif /* !WINDOWSNT */
  356.       if (XTYPE (sym) == Lisp_Symbol)
  357.         {
  358.           /* Attach a docstring to a variable?  */
  359.           if (p[1] == 'V')
  360.         {
  361.           /* Install file-position as variable-documentation property
  362.              and make it negative for a user-variable
  363.              (doc starts with a `*').  */
  364.           Fput (sym, Qvariable_documentation,
  365.             make_number ((pos + end + 1 - buf)
  366.                      * (end[1] == '*' ? -1 : 1)));
  367.         }
  368.  
  369.           /* Attach a docstring to a function?  */
  370.           else if (p[1] == 'F')
  371.         store_function_docstring (sym, pos + end + 1 - buf);
  372.           else
  373.         error ("DOC file invalid at position %d", pos);
  374.         }
  375.     }
  376.       pos += end - buf;
  377.       filled -= end - buf;
  378.       bcopy (end, buf, filled);
  379.     }
  380.   close (fd);
  381.   return Qnil;
  382. }
  383.  
  384. DEFUN ("substitute-command-keys", Fsubstitute_command_keys,
  385.   Ssubstitute_command_keys, 1, 1, 0,
  386.   "Substitute key descriptions for command names in STRING.\n\
  387. Return a new string which is STRING with substrings of the form \\=\\[COMMAND]\n\
  388. replaced by either:  a keystroke sequence that will invoke COMMAND,\n\
  389. or \"M-x COMMAND\" if COMMAND is not on any keys.\n\
  390. Substrings of the form \\=\\{MAPVAR} are replaced by summaries\n\
  391. \(made by describe-bindings) of the value of MAPVAR, taken as a keymap.\n\
  392. Substrings of the form \\=\\<MAPVAR> specify to use the value of MAPVAR\n\
  393. as the keymap for future \\=\\[COMMAND] substrings.\n\
  394. \\=\\= quotes the following character and is discarded;\n\
  395. thus, \\=\\=\\=\\= puts \\=\\= into the output, and \\=\\=\\=\\[ puts \\=\\[ into the output.")
  396.   (str)
  397.      Lisp_Object str;
  398. {
  399.   unsigned char *buf;
  400.   int changed = 0;
  401.   register unsigned char *strp;
  402.   register unsigned char *bufp;
  403.   int idx;
  404.   int bsize;
  405.   unsigned char *new;
  406.   Lisp_Object tem;
  407.   Lisp_Object keymap;
  408.   unsigned char *start;
  409.   int length;
  410.   Lisp_Object name;
  411.   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  412.  
  413.   if (NILP (str))
  414.     return Qnil;
  415.  
  416.   CHECK_STRING (str, 0);
  417.   tem = Qnil;
  418.   keymap = Qnil;
  419.   name = Qnil;
  420.   GCPRO4 (str, tem, keymap, name);
  421.  
  422.   keymap = current_buffer->keymap;
  423.  
  424.   bsize = XSTRING (str)->size;
  425.   bufp = buf = (unsigned char *) xmalloc (bsize);
  426.  
  427.   strp = (unsigned char *) XSTRING (str)->data;
  428.   while (strp < (unsigned char *) XSTRING (str)->data + XSTRING (str)->size)
  429.     {
  430.       if (strp[0] == '\\' && strp[1] == '=')
  431.     {
  432.       /* \= quotes the next character;
  433.          thus, to put in \[ without its special meaning, use \=\[.  */
  434.       changed = 1;
  435.       *bufp++ = strp[2];
  436.       strp += 3;
  437.     }
  438.       else if (strp[0] == '\\' && strp[1] == '[')
  439.     {
  440.       changed = 1;
  441.       strp += 2;        /* skip \[ */
  442.       start = strp;
  443.  
  444.       while ((strp - (unsigned char *) XSTRING (str)->data
  445.           < XSTRING (str)->size)
  446.          && *strp != ']')
  447.         strp++;
  448.       length = strp - start;
  449.       strp++;        /* skip ] */
  450.  
  451.       /* Save STRP in IDX.  */
  452.       idx = strp - (unsigned char *) XSTRING (str)->data;
  453.       tem = Fintern (make_string (start, length), Qnil);
  454.       tem = Fwhere_is_internal (tem, keymap, Qnil, Qt, Qnil);
  455.  
  456.       if (NILP (tem))    /* but not on any keys */
  457.         {
  458.           new = (unsigned char *) xrealloc (buf, bsize += 4);
  459.           bufp += new - buf;
  460.           buf = new;
  461.           bcopy ("M-x ", bufp, 4);
  462.           bufp += 4;
  463.           goto subst;
  464.         }
  465.       else
  466.         {            /* function is on a key */
  467.           tem = Fkey_description (tem);
  468.           goto subst_string;
  469.         }
  470.     }
  471.       /* \{foo} is replaced with a summary of the keymap (symbol-value foo).
  472.      \<foo> just sets the keymap used for \[cmd].  */
  473.       else if (strp[0] == '\\' && (strp[1] == '{' || strp[1] == '<'))
  474.     {
  475.       struct buffer *oldbuf;
  476.  
  477.       changed = 1;
  478.       strp += 2;        /* skip \{ or \< */
  479.       start = strp;
  480.  
  481.       while ((strp - (unsigned char *) XSTRING (str)->data
  482.           < XSTRING (str)->size)
  483.          && *strp != '}' && *strp != '>')
  484.         strp++;
  485.       length = strp - start;
  486.       strp++;            /* skip } or > */
  487.  
  488.       /* Save STRP in IDX.  */
  489.       idx = strp - (unsigned char *) XSTRING (str)->data;
  490.  
  491.       /* Get the value of the keymap in TEM, or nil if undefined.
  492.          Do this while still in the user's current buffer
  493.          in case it is a local variable.  */
  494.       name = Fintern (make_string (start, length), Qnil);
  495.       tem = Fboundp (name);
  496.       if (! NILP (tem))
  497.         {
  498.           tem = Fsymbol_value (name);
  499.           if (! NILP (tem))
  500.         tem = get_keymap_1 (tem, 0, 1);
  501.         }
  502.  
  503.       /* Now switch to a temp buffer.  */
  504.       oldbuf = current_buffer;
  505.       set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
  506.  
  507.       if (NILP (tem))
  508.         {
  509.           name = Fsymbol_name (name);
  510.           insert_string ("\nUses keymap \"");
  511.           insert_from_string (name, 0, XSTRING (name)->size);
  512.           insert_string ("\", which is not currently defined.\n");
  513.           if (start[-1] == '<') keymap = Qnil;
  514.         }
  515.       else if (start[-1] == '<')
  516.         keymap = tem;
  517.       else
  518.         describe_map_tree (tem, 1, Qnil, Qnil, 0);
  519.       tem = Fbuffer_string ();
  520.       Ferase_buffer ();
  521.       set_buffer_internal (oldbuf);
  522.  
  523.     subst_string:
  524.       start = XSTRING (tem)->data;
  525.       length = XSTRING (tem)->size;
  526.     subst:
  527.       new = (unsigned char *) xrealloc (buf, bsize += length);
  528.       bufp += new - buf;
  529.       buf = new;
  530.       bcopy (start, bufp, length);
  531.       bufp += length;
  532.       /* Check STR again in case gc relocated it.  */
  533.       strp = (unsigned char *) XSTRING (str)->data + idx;
  534.     }
  535.       else            /* just copy other chars */
  536.     *bufp++ = *strp++;
  537.     }
  538.  
  539.   if (changed)            /* don't bother if nothing substituted */
  540.     tem = make_string (buf, bufp - buf);
  541.   else
  542.     tem = str;
  543.   xfree (buf);
  544.   RETURN_UNGCPRO (tem);
  545. }
  546.  
  547. _VOID_
  548. syms_of_doc ()
  549. {
  550.   DEFVAR_LISP ("internal-doc-file-name", &Vdoc_file_name,
  551.     "Name of file containing documentation strings of built-in symbols.");
  552.   Vdoc_file_name = Qnil;
  553.  
  554.   defsubr (&Sdocumentation);
  555.   defsubr (&Sdocumentation_property);
  556.   defsubr (&Ssnarf_documentation);
  557.   defsubr (&Ssubstitute_command_keys);
  558. }
  559.